package org.jzs.mybaseapp.section.home.costomview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import org.jzs.mybaseapp.R;
import org.jzs.mybaseapp.common.widget.expandtab.ViewBaseAction;
import org.jzs.mybaseapp.section.home.entity.ExpandTabEntity.DataEntity.AreaEntity;
import org.jzs.mybaseapp.section.home.entity.ExpandTabEntity.DataEntity.AreaEntity.RoadEntity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 区域
 *
 * @author Zhanqx
 */
public class QyView extends LinearLayout implements ViewBaseAction {

    private ListView regionListView;
    private ListView plateListView;
    private ArrayList<String> groupNames = new ArrayList<String>();
    private ArrayList<String> groupIds = new ArrayList<String>();
    private LinkedList<String> childrenNames = new LinkedList<String>();
    private LinkedList<String> childrenIds = new LinkedList<String>();
    private SparseArray<LinkedList<String>> children = new SparseArray<LinkedList<String>>();
    private SparseArray<LinkedList<String>> children_id = new SparseArray<LinkedList<String>>();
    private ChildTypeAdapter plateListViewAdapter;
    private GroupTypeAdapter earaListViewAdapter;
    private OnSelectListener mOnSelectListener;
    private int tEaraPosition = 0;
    private int tBlockPosition = 0;
    private String childName = "不限";
    private String childId;
    private String groupName;
    private String groupId;
    private List<AreaEntity> typeLst;

    public QyView(Context context, List<AreaEntity> typeLst) {
        super(context);
        this.typeLst = typeLst;
        init(context);
    }

    public QyView(Context context, AttributeSet attrs, List<AreaEntity> typeLst) {
        super(context, attrs);
        this.typeLst = typeLst;
        init(context);
    }

    public void updateShowText(String showArea, String showBlock) {
        if (showArea == null || showBlock == null) {
            return;
        }
        for (int i = 0; i < groupNames.size(); i++) {
            if (groupNames.get(i).equals(showArea)) {
                earaListViewAdapter.setSelectedPosition(i);
                childrenNames.clear();
                if (i < children.size()) {
                    childrenNames.addAll(children.get(i));
                    childrenIds.addAll(children_id.get(i));
                }
                tEaraPosition = i;
                break;
            }
        }
        for (int j = 0; j < childrenNames.size(); j++) {
            if (childrenNames.get(j).replace("不限", "").equals(showBlock.trim())) {
                plateListViewAdapter.setSelectedPosition(j);
                tBlockPosition = j;
                break;
            }
        }
        setDefaultSelect();
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_region, this, true);
        regionListView = (ListView) findViewById(R.id.listView);
        plateListView = (ListView) findViewById(R.id.listView2);
        // setBackgroundDrawable(getResources().getDrawable(
        // R.drawable.choosearea_bg_left));

        if (typeLst == null) {
            typeLst = new ArrayList<AreaEntity>();
        }
        List<AreaEntity> newLst = new ArrayList<AreaEntity>();//区域集合
        AreaEntity data = new AreaEntity();
        data.areaId = "";
        data.leaf = false;
        data.areaName = "全部区域";


        RoadEntity childData = new RoadEntity();
        childData.roadId = "";
        childData.leaf = true;
        childData.roadName = "全部";

        List<RoadEntity> mdatas = new ArrayList<RoadEntity>();//子区域集合
        mdatas.add(childData);
        data.road = mdatas;
        newLst.add(data);

        for (int i = 0; i < typeLst.size(); i++) {
            newLst.add(typeLst.get(i));
        }


        for (int i = 0; i < newLst.size(); i++) {
            if (i == 0) {
                groupName = newLst.get(i).areaName;
                groupId = newLst.get(i).areaId;
            }
            groupNames.add(newLst.get(i).areaName);
            groupIds.add(newLst.get(i).areaId);
            LinkedList<String> item = new LinkedList<String>();
            LinkedList<String> itemId = new LinkedList<String>();
            List<RoadEntity> children2 = newLst.get(i).road;
            if (children2 != null && children2.size() > 0) {
                for (int j = 0; j < children2.size(); j++) {
                    item.add(children2.get(j).roadName);
                    itemId.add(children2.get(j).roadId);
                }
            }

            children.put(i, item);
            children_id.put(i, itemId);
        }

        earaListViewAdapter = new GroupTypeAdapter(context, groupNames, R.drawable.choose_item_selected, R.drawable.choose_eara_item_selector);
        earaListViewAdapter.setTextSize(14);
        earaListViewAdapter.setSelectedPositionNoNotify(tEaraPosition);
        regionListView.setAdapter(earaListViewAdapter);
        earaListViewAdapter.setOnItemClickListener(new GroupTypeAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                if (position < children.size()) {
                    groupName = groupNames.get(position);
                    groupId = groupIds.get(position);
                    childrenNames.clear();
                    childrenNames.addAll(children.get(position));
                    childrenIds.clear();
                    childrenIds.addAll(children_id.get(position));
                    plateListViewAdapter.notifyDataSetChanged();
                }
            }
        });
        if (tEaraPosition < children.size())
            childrenNames.addAll(children.get(tEaraPosition));
        childrenIds.addAll(children_id.get(tEaraPosition));
        plateListViewAdapter = new ChildTypeAdapter(context, childrenNames, R.drawable.choose_item_right, R.drawable.choose_plate_item_selector);
        plateListViewAdapter.setTextSize(14);
        plateListViewAdapter.setSelectedPositionNoNotify(tBlockPosition);
        plateListView.setAdapter(plateListViewAdapter);
        plateListViewAdapter.setOnItemClickListener(new ChildTypeAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, final int position) {

                childName = childrenNames.get(position);
                childId = childrenIds.get(position);
                if (mOnSelectListener != null) {
                    mOnSelectListener.getValue(groupName, groupId, childName, childId, position);
                }
            }
        });
        if (tBlockPosition < childrenNames.size())
            childName = childrenNames.get(tBlockPosition);
        childId = childrenIds.get(tBlockPosition);
        if (childName.contains("不限")) {
            childName = childName.replace("不限", "");
        }
        setDefaultSelect();

    }

    public void setDefaultSelect() {
        regionListView.setSelection(tEaraPosition);
        plateListView.setSelection(tBlockPosition);
    }

    public String getShowText() {
        return childName;
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        void getValue(String gourValue, String groupId, String ChildValue, String childId, int position);
    }

    @Override
    public void hide() {

    }

    @Override
    public void show() {

    }
}
